import javax.swing.*;
import java.awt.*;
import java.io.File;
import java.util.ArrayList;

/**
 * @author fengbo
 */
public class CManager extends JFrame {

    private JTextField txtFile;
    private JComboBox<File> cmb;
    private JTextArea ta;

    public CManager(java.util.List<String> list) {
        setTitle("文件管理系统");
        // resizeable值为true时，表示生成的窗体可以自由改变大小；
        // 为false时，表示生成的窗体大小是由程序员决定的，用户不可以自由改变该窗体的大小。
        setResizable(false);
        // 用户单击窗口的关闭按钮时程序执行的操作
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        // 设置位置
        setLocationRelativeTo(null);

        setLayout(new BorderLayout());
        FlowLayout fl = new FlowLayout();

        JPanel jp1 = new JPanel();
        JPanel jp2 = new JPanel();
        jp1.setLayout(fl);
        jp2.setLayout(fl);

        getContentPane().add("North", jp1);
        getContentPane().add("Center", jp2);

        cmb = new JComboBox<>();
        for (String file : list) {
            cmb.addItem(new File(file));
        }
        jp1.add(cmb);
        cmb.addActionListener(e -> {
            Object source = e.getSource();
            Object item;
            if (source == cmb && (item = cmb.getSelectedItem()) != null) {

//                listFile(item.toString());
            }
        });

        txtFile = new JTextField(20);
        txtFile.setText("");

        jp1.add(txtFile);
        JButton btnList = new JButton("下载");
        btnList.addActionListener(e -> {
            Object source = e.getSource();
            String filename = txtFile.getText();
            System.out.println(filename);
        });
        jp1.add(btnList);

        ta = new JTextArea(15,66);
        JScrollPane sp = new JScrollPane(ta);
        jp2.add(sp);

        setVisible(true);

        pack();
    }

    private void listFile(String filename){
        File file = new File(filename);
        if (!file.exists() || !file.isDirectory()) {
            return;
        }
        File[] dirList = file.listFiles();
        if (dirList != null && dirList.length > 0) {
            java.util.List<String> dirStrList = new ArrayList<>(dirList.length);
            for (File value : dirList) {
                if (value.isDirectory()) {
                    dirStrList.add(value.getAbsolutePath());
                } else {
                    ta.append(value.getAbsolutePath() + "\r\n");
                }
            }
            if (dirStrList.size() > 0) {
                cmb.removeAllItems();
                for (String str : dirStrList) {
                    cmb.addItem(new File(str));
                }
            }
        } else {
            ta.removeAll();
        }
    }

    public static void main(String[] args) {
//        new CManager();
    }

}